package com.yanqu.road.web.controller.game;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.server.manger.system.PlatformGoodsMgr;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.config.Config;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(urlPatterns = "/queryGoodsNameList")
public class QueryGoodsNameList extends BaseJsonHttpServlet {

    private static Logger logger = LogManager.getLogger(QueryGoodsNameList.class.getName());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long serverId = ConfigHelper.getLong("serverId");
        int channelId = ServerListMgr.getChannelId(serverId);
        logger.info("request QueryGoodsNameList");
        HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
        JSONObject paramObj = httpJsonWebParam.getParamObj();
        Map<Integer, String> result = new HashMap<>();
        String language = "zh_cn";
        if(Config.isDebug() || channelId == eChannelType.TaiWan.getValue()){
            language = "zh_tw";
        }
        for (GoodsInfo goodsInfo : CommonMgr.getGoodsInfoList()) {
            result.put(goodsInfo.getGoodsId(), ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language));
        }
        for (GoodsInfo goodsInfo : PlatformGoodsMgr.getGoodsInfoList()) {
            result.put(goodsInfo.getGoodsId(), ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language));
        }
        for (Map.Entry<Integer, GoodsInfo> entry : GoodsMgr.getGoodsInfoMap().entrySet()) {
            GoodsInfo goodsInfo = entry.getValue();
            result.put(goodsInfo.getGoodsId(), MultipleLanguageMgr.getContent(goodsInfo.getGoodsName(), language));
        }
        sendMessage(response, JSON.toJSONString(result));
    }
}
